<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.mongodb.client.MongoCursor,org.bson.Document" %>
<%@ page import="mongo.UserAccount,mongo.CourseClass, mongo.TeachingMaterialsTemplate" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加任务</title>
<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
	<jsp:include page="../navbar.html" />
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-1 col-md-offset-3">
				<a href="taskServlet?method=list">
					<button type="button" class="btn btn-default"><em class="fa fa-chevron-left"></em> 返回</button>
				</a>
			</div>
		</div>
    	<div class="row mt15">
			<div class="col-md-6 col-md-offset-3">
				<div class="panel">
                	<div class="panel-body">
                  		<h4 class="page-header mt0">发布任务</h4>
                  		<form name="taskform" role="form" action="taskServlet" method="post"  onsubmit="return check(this);" class="form-horizontal mt20">
	                  	    <fieldset>
				              <div class="form-group">
				                  <label class="col-md-2 control-label">任务标题</label>
				                  <div class="col-md-9">
				                     <input class="form-control" type="text" name="name" placeholder="请输入任务标题" />
				                  </div>
				               </div> 
			                </fieldset>
		               		<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">任务内容</label>
		                  			<div class="col-md-9">
		                  				<textarea class="form-control no-resize" name="content" placeholder="请输入任务内容" rows="6"></textarea>
		                  			</div>
		               			</div>	
		               		</fieldset>  
		               		<fieldset>
		               			 <div class="form-group">
					               <label class="col-sm-2 control-label">指定教师</label>
					               <div class="col-sm-9">
					                  <select class="form-control" name="teachercode" id="selectTeacher">
					                  	<option selected>请选择教师</option>
						               <%
						               		UserAccount UserAccount = new UserAccount();
						               		MongoCursor<Document> result = UserAccount.listTeacherList(); 
					                  		while(result.hasNext()) {
					                  			Document document = result.next();%>
					                     	<option value="<%=document.getString("username") %>"><%=document.getString("name") %></option>
					                    <%} %> 
					                  </select>
					              </div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group">
			               			<label class="col-md-2 control-label">课程</label>
				               		<div class="col-md-9">
			               				<select class="form-control" name="coursecode" id="selectcourse">
						               		<option selected>请选择课程</option>	
					                  	</select>
			               			</div>
			               		</div>
		               		</fieldset>
		               		<fieldset>
		               			 <div class="form-group">
					               <label class="col-sm-2 control-label">材料模板</label>
					               <div class="col-sm-9">
					                  <select class="form-control" name="templateid" id="selectTemplate">
						               <%
						               		TeachingMaterialsTemplate TeachingMaterialsTemplate = new TeachingMaterialsTemplate();
						               		MongoCursor<Document> result1 = TeachingMaterialsTemplate.list(); 
					                  		while(result1.hasNext()) {
					                  			Document document = result1.next();%>
					                     	<option value="<%=document.getObjectId("_id") %>"><%=document.getString("name") %></option>
					                    <%} %> 
					                  </select>
					              </div>
		               		</fieldset> 	               		
		               		<input type="hidden" name="teachername" id="teachername">
		               		<input type="hidden" name="courseclassname" id="courseclassname">
		               		<input type="hidden" name="templatename" id="templatename">
		               		<div class="col-md-10 col-md-offset-1">
		               			<div class="col-md-6">
		               				<button type="submit" class="btn btn-primary btn-block" name="method" value="add">添加</button> 
		               			</div>
		               			<div class="col-md-6">
		               				<button type="reset" class="btn btn-default btn-block">重置</button> 
		               			</div>
		               		</div>	               		
              			</form>
                  	</div>
                 </div>
            </div>
         </div>
     </div>
     <script>
		$(document).ready(function(){
			//专业下拉选择框变化
			$("#selectTeacher").change(function(){  
				var checkValue=$("#selectTeacher").val();  
				$.ajax({
		             type: "post",
		             url: "taskServlet?method=getCourseClass&teachercode="+checkValue,
		             dataType: "text",
		             success: function(data){
		            	 var result = JSON.parse(data); 
		            	 $("#selectcourse").empty(); //empty()方法删除被选元素的子元素（option）
		            	 if(result.length<1) {
		            		 $("#selectcourse").append("<option>该教师没有课程班</option>");
		            	 }
		            	 for(var i=0; i<result.length;i++){              
		                     $("#selectcourse").append("<option value='"+result[i].coursecode+"'>"+result[i].name+"</option>");
		            	 }
		             }, error: function(){
		            	 swal("请求出错","请重新选择","warning");
		             }
		       })
			});
		});
		function check(taskform) {  
			taskform.teachername.value =$("#selectTeacher").find("option:selected").text(); 
			taskform.courseclassname.value =$("#selectcourse").find("option:selected").text();  
			taskform.templatename.value =$("#selectTemplate").find("option:selected").text();  
			if(taskform.name.value=="") {  
		        swal("任务标题不能为空","请重新输入","warning");  
		        return false;  
		    } 
			else if(taskform.content.value=="") {  
		        swal("任务内容不能为空","请重新输入","warning");  
		        return false;  
		    }  
			else if(taskform.teachername.value=="请选择教师") {
				swal("教师不能为空","请重新选择","warning");
				return false;  
			}
			else if(taskform.courseclassname.value=="请选择课程" || taskform.courseclassname.value=="该教师没有课程班") {
				swal("课程不能为空","请重新选择","warning");
				return false;  
			}
		
		    return true;  
		}   
	</script>
</body>
</html>